package wyu.controller;

import com.dto.Result;
import com.entity.PostBar;
import com.entity.User;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import wyu.service.PostBarService;
import wyu.service.feign.UserBarService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/postBar")
public class PostBarController {
    @Resource
    PostBarService postBarService;

    @GetMapping("/admin/getList")
    public Result getList(){
        return postBarService.getlist();
    }
    @PostMapping("/admin/add")
    public Result add(@RequestParam("name")String name,
                      @RequestParam("checkedCategories")String categoryId,
                      @Nullable @RequestParam("describe") String describe,
                      @Nullable @RequestParam("file") MultipartFile image){
        System.out.println(name+"@"+categoryId+"@"+describe+"@"+image);
        return postBarService.add(name,categoryId,describe,image);
    }
    @GetMapping("/admin/del")
    public Result del(@RequestParam("id")int id){
        boolean remove = postBarService.removeById(id);
        if (!remove){
            return Result.fail(900,"删除失败");
        }
        return Result.ok();
    }
    @PatchMapping("/admin/setRecommendBars")
    public Result setRecommendBars(@RequestParam("barIds")List<Integer> barIds){
        return postBarService.setRecommendBars(barIds);
    }
    /**
     * 根据条件（参数可以选择性发送）获取贴吧列表
     * 获取 贴吧列表信息的同时 获取当前登录用户（session中的user）和各贴吧的加入关系
     * @param request req
     * @param getRecommendBars 获取推荐贴吧 1获取推荐  0正常获取所有贴吧（默认）
     * @param unameIndex 查询某用户加入的贴吧
     * @param searchWd 搜索贴吧（按名称或 贴吧id 搜索）
     * @param searchType 0贴吧名称（默认）  1贴吧编号
     * @param sortType postsNum 帖子数最多倒序  peopleNum 人数最多倒序  否则按贴吧id
     * @param categoryId 查询该分类下的贴吧
     * @return 符合条件的贴吧列表 以及 当前用户加入
     * todo 该方法功能未全部完成
     */
    @GetMapping("getRvcBarList")
    public Result getRvcBarList(HttpServletRequest request,
                                             @RequestParam(value = "getRec", required = false, defaultValue = "0") Integer getRecommendBars,
                                             @RequestParam(value = "uname", required = false) String unameIndex,
                                             @RequestParam(value = "searchWd", required = false) String searchWd,
                                             @RequestParam(value = "searchType", required = false, defaultValue = "0") Integer searchType,
                                             @RequestParam(value = "sortType", required = false, defaultValue = "postsNum") String sortType,
                                             @RequestParam(value = "ctgId", required = false) Integer categoryId){

        return postBarService.getRvcBarList(getRecommendBars,unameIndex,searchWd,searchType,sortType,categoryId);
    }
    @GetMapping("/getDetailedBar")
    public Result getDetailedBar(@RequestParam("barId")int barId,
                                 HttpServletRequest request){
        User user = (User) request.getSession().getAttribute("user");
        //未登录
        if(user==null){
            return postBarService.getDetailedBar(null,barId);
        }
        //已登录
        String uname = user.getUname();
        System.out.println(uname);
        return postBarService.getDetailedBar(uname,barId);
    }
    @GetMapping("/getListByIdList")
    public List<PostBar> getListByIdList(@RequestParam("idList")List<Integer> list){
        List<PostBar> postBars = postBarService.query().in("ID", list).list();
        return postBars;
    }
    @GetMapping("/getById")
    public PostBar getById(@RequestParam("barId")int id){
        return postBarService.getById(id);
    }
    @GetMapping("/joinBar")
    public Result joinBar (@RequestParam("id")int barId,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(("user"));
        if (user==null){
            return Result.fail(503,"请先登录");
        }
        String uname = user.getUname();
        return postBarService.joinBar(barId,uname);
    }
    @GetMapping("/exitBar")
    public Result exitBar (@RequestParam("id")int barId,HttpServletRequest request){
        User user = (User) request.getSession().getAttribute(("user"));
        if (user==null){
            return Result.fail(503,"请先登录");
        }
        String uname = user.getUname();
        return postBarService.exitBar(barId,uname);
    }
}
